﻿using System;

using System.Collections.Generic;
using System.Text;
using Silvermoon.Controls;
using System.Drawing;
using System.Diagnostics;
using Silvermoon.Controls.Interfaces;
using Silvermoon.Core;

namespace Silvermoon.Controls.Base
{
    public class Decorator : CompositeControlBase, IDecorator
    {
        protected override void Dispose(bool dispose)
        {
            Control c = this.Control;
            if (c != null) c.Dispose();
            VisibleControls.ClearQuick();
            base.Dispose(dispose);
        }
        public Control Control
        {
            get { return VisibleControls.First; }
            set
            {
                if (VisibleControls.First != value) OnControlChange(value);
            }
        }

        protected virtual void OnControlChange(Control value)
        {
            VisibleControls.Clear();
            if (value != null) VisibleControls.AddFirst(value);
            Invalidate(InvalidationFlag.LayoutAppearance);
        }

        protected override void OnLayout()
        {
            base.OnLayout();
            if (CheckFlag(InvalidationFlag.Size | InvalidationFlag.Layout))
            {
                Control c = Control;
                if (c != null) Layouter.Layout(c, Size); //c.Bounds = new Rectangle(0, 0, Width, Height);
            }
        }
    }
}
